//
//  DDWelcomeViewController.m
//  DingDongZP
//
//  Created by Jyh on 16/3/19.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

#import "GRKPageViewController.h"
#import "HMSegmentedControl.h"

#import "DDWelcomeViewController.h"
#import "DDDiscoverViewController.h"
#import "DDWelcomeJobsViewController.h"
#import "DDWelcomeJobFindersViewController.h"
#import "DDLoginViewController.h"
#import "DDRegisterViewController.h"
#import "DDLoginManager.h"

@interface DDWelcomeViewController () <GRKPageViewControllerDelegate, GRKPageViewControllerDataSource>

@property (weak, nonatomic) IBOutlet UIView *bottomView;
@property (weak, nonatomic) IBOutlet UIButton *registerButton;
@property (weak, nonatomic) IBOutlet UIButton *loginButton;

@property (strong, nonatomic) GRKPageViewController *pageController;
@property (strong, nonatomic) HMSegmentedControl *segmentedControl;
@property (strong, nonatomic) NSMutableArray *viewControllers;

@property (strong, nonatomic) DDWelcomeJobsViewController *jobsVC;
@property (strong, nonatomic) DDWelcomeJobFindersViewController *jobfindersVC;

@property (strong, nonatomic) UIView *lineView;

- (IBAction)onRegisterClick:(UIButton *)sender;
- (IBAction)onLoginClick:(UIButton *)sender;
@end

@implementation DDWelcomeViewController
{
    UIView *windowShadowView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpData];
    [self setUpView];
    //    [self setUPNav];
    
    [self.pageController setCurrentIndex:0];
    
    /**
     *  通知 控制弹窗的出现
     */
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(notificateToPopView:) name:@"popView" object:nil];
}

- (void)setUPNav {
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
    self.navigationController.navigationBar.tintColor = COLOR_NAVIGATIONBAR;
    
}

- (void)setUpData
{
    self.viewControllers = [NSMutableArray array];
    self.jobsVC = [[DDWelcomeJobsViewController alloc] init];
    self.jobfindersVC = [[DDWelcomeJobFindersViewController alloc] init];
    
    [self.viewControllers addObject:self.jobsVC];
    [self.viewControllers addObject:self.jobfindersVC];
    
}

- (void)setUpView
{
    
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    GRKPageViewController *pageViewController = [[GRKPageViewController alloc] init];
    pageViewController.view.backgroundColor = COLOR_BACKGROUND;
    pageViewController.dataSource = self;
    pageViewController.delegate = self;
    
    [self addChildViewController:pageViewController];
    [self.view addSubview:pageViewController.view];
    
    WEAKSELF
    [pageViewController.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view.mas_top);
        make.width.equalTo(weakSelf.view);
        make.bottom.equalTo(weakSelf.view).offset(-49);
    }];
    
    self.pageController = pageViewController;
    
    // segmentControl
    self.segmentedControl = [[HMSegmentedControl alloc] initWithSectionTitles:@[@"职位", @"牛人"]];
    self.segmentedControl.backgroundColor = [UIColor clearColor];
    self.segmentedControl.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:16]};
    self.segmentedControl.selectionIndicatorHeight = 1.f;
    self.segmentedControl.selectionIndicatorColor = [UIColor whiteColor];
    self.segmentedControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
    [self.segmentedControl setIndexChangeBlock:^(NSInteger index) {
        [weakSelf.pageController setCurrentIndex:index animated:YES];
    }];
    self.segmentedControl.frame = CGRectMake(0, 0, 150, 40);
    [self.segmentedControl addTarget:self action:@selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];
    
    [self.navigationItem setTitleView:self.segmentedControl];
    
    
    self.lineView = [[UIView alloc] init];
    self.lineView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.lineView];
    
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@0.5);
        make.centerX.equalTo(weakSelf.bottomView);
        make.top.equalTo(weakSelf.bottomView).offset(5);
        make.bottom.equalTo(weakSelf.bottomView).offset(-5);
    }];
}

#pragma mark
#pragma mark - Action

- (void)segmentedControlChangedValue:(HMSegmentedControl *)segmentedControl {
    
    //    NSLog(@"Selected index %ld (via UIControlEventValueChanged)", (long)segmentedControl.selectedSegmentIndex);
}

- (IBAction)onRegisterClick:(UIButton *)sender {
    
    //    DDRegisterViewController *registerVC = [[DDRegisterViewController alloc] init];
    //    [self.navigationController pushViewController:registerVC animated:true];
    LDRegisterVC *vc = [[LDRegisterVC alloc] init];
    [self.navigationController pushViewController:vc animated:true];
}

- (IBAction)onLoginClick:(UIButton *)sender {
    
    //    LDLoginBaseVC *loginVC = [[LDLoginBaseVC alloc] init];
    //    [self.navigationController pushViewController:loginVC animated:YES];
    [self setupLoginViewController];
}

- (void)setupLoginViewController
{
    if (windowShadowView) {
        [windowShadowView removeFromSuperview];
    }
    
    WEAKSELF
    [[DDLoginManager sharedManager] showLoginViewControllerFromViewController:self showCancelButton:NO success:^{
        //    DDLoginViewController *loginVC = [[DDLoginViewController alloc] init];
        //    [self.navigationController pushViewController:loginVC animated:YES];
        [weakSelf jumpToMainVC];
        
    } failure:^(NSString *errorMessage) {
        
    } cancel:^{
        
    }];
}

- (void)jumpToMainVC
{
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    if ([DDClientInfoManager clientType] == DDClientTypeCattle) {
        
        [delegate initCattleTabbarController];
    } else {
        
        [delegate initBossTabbarController];
    }
    
}

#pragma mark - GRKPageViewControllerDataSource

- (NSUInteger)pageCountForPageViewController:(GRKPageViewController *)controller {
    
    return self.viewControllers.count;
}
- (UIViewController *)viewControllerForIndex:(NSUInteger)index forPageViewController:(GRKPageViewController *)controller {
    
    UIViewController *viewController = nil;
    
    if (index < self.viewControllers.count) {
        viewController = self.viewControllers[index];
    }
    
    if (!viewController) {
        viewController = [[DDBaseViewController alloc] init];
    }
    
    return viewController;
}

#pragma mark - GRKPageViewControllerDelegate

- (void)changedIndex:(NSUInteger)index forPageViewController:(GRKPageViewController *)controller {
    
    [self.segmentedControl setSelectedSegmentIndex:index];
}

- (void)changedIndexOffset:(CGFloat)indexOffset forPageViewController:(GRKPageViewController *)controller {
    
}

/**
 *  出现登录弹窗
 */
- (void)notificateToPopView:(NSNotification *)not
{
    [self creatPopView];
}

- (void)creatPopView
{
    WEAKSELF
    windowShadowView = [[UIView alloc] init];
    windowShadowView.userInteractionEnabled = YES;
    windowShadowView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    windowShadowView.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.3500];
    [weakSelf.view addSubview:windowShadowView];
    
    UIView *backgroundView = [[UIView alloc]init];
    backgroundView.backgroundColor = [UIColor whiteColor];
    backgroundView.userInteractionEnabled = YES;
    backgroundView.layer.masksToBounds = YES;
    backgroundView.layer.cornerRadius = 10;
    [windowShadowView addSubview:backgroundView];
    [backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(230.5));
        make.height.equalTo(@(264.5));
        make.centerX.equalTo(weakSelf.view);
        make.centerY.equalTo(weakSelf.view).offset(-27);
    }];
    
    
    UIImageView *cancleButtonImageView = [[UIImageView alloc]init];
    cancleButtonImageView.image = [UIImage imageNamed:@"lALOYP1PJBsb"];
    [windowShadowView addSubview:cancleButtonImageView];
    [cancleButtonImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(10));
        make.height.equalTo(@(10));
        make.centerX.equalTo(backgroundView.mas_right).offset(-15);
        make.centerY.equalTo(backgroundView.mas_top).offset(15);
    }];
    
    UIButton *cancleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancleButton addTarget:self action:@selector(cancleAction) forControlEvents:UIControlEventTouchUpInside];
    [windowShadowView addSubview:cancleButton];
    [cancleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(50));
        make.height.equalTo(@(50));
        make.top.equalTo(backgroundView.mas_top);
        make.right.equalTo(backgroundView.mas_right);
        
    }];
    UIImageView *iconView = [[UIImageView alloc]init];
    [backgroundView addSubview:iconView];
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(80, 80));
        make.top.equalTo(backgroundView).offset(40);
        make.centerX.equalTo(backgroundView);
        
    }];
    iconView.layer.cornerRadius = 20;
    iconView.layer.masksToBounds = YES;
    iconView.image = [UIImage imageNamed:@"launch_logo"];
    
    UILabel *messageLabel = [[UILabel alloc]init];
    messageLabel.textColor = COLOR_FONT_LIGHT_GREEN;
    messageLabel.text = @"登录叮咚快聘,直接开聊";
    messageLabel.textAlignment = 1;
    [backgroundView addSubview:messageLabel];
    [messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(iconView.mas_bottom).offset(30);
        make.left.equalTo(@(0));
        make.right.equalTo(backgroundView);
        make.height.equalTo(@(20));
    }];
    
    UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    loginButton.backgroundColor = COLOR_FONT_LIGHT_GREEN;
    loginButton.layer.masksToBounds = YES;
    loginButton.layer.cornerRadius = 5;
    [loginButton setTitle:@"登录开聊" forState:UIControlStateNormal];
    [loginButton addTarget:self action:@selector(setupLoginViewController) forControlEvents:UIControlEventTouchUpInside];
    [backgroundView addSubview:loginButton];
    [loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(messageLabel.mas_bottom).offset(30);
        make.width.equalTo(@(149));
        make.height.equalTo(@(37));
        make.centerX.equalTo(backgroundView);
    }];
}

- (void)cancleAction
{
    [windowShadowView removeFromSuperview];
}

/**
 *  销毁通知
 */
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"popView" object:nil];
}
@end
